package cn.tecnova.utils

import java.lang.Exception
import java.net.InetAddress

import org.elasticsearch.common.settings.Settings
import org.elasticsearch.common.transport.InetSocketTransportAddress
import org.elasticsearch.transport.client.PreBuiltTransportClient

/**
  * description:Es工具类
  **/
object ESUtils {

  private lazy val settings = Settings.builder().put("cluster.name", ConfigHandler.clusterName).build()

  def getEsClient() ={

    var client: PreBuiltTransportClient = null

    try {
      client = new PreBuiltTransportClient(settings)
      //      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConfigHandler.esHost), ConfigHandler.esPort))
      client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConfigHandler.esHost1), ConfigHandler.esTCPPort))
      client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConfigHandler.esHost2), ConfigHandler.esTCPPort))
      client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConfigHandler.esHost3), ConfigHandler.esTCPPort))
//      client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConfigHandler.esHost4), ConfigHandler.esTCPPort))
//      client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConfigHandler.esHost5), ConfigHandler.esTCPPort))
    }catch {

      case e: Exception => e.printStackTrace()

    }

    client
  }

}
